package com.yanqu.road.logic.pb;

import com.yanqu.road.entity.activity.langyabang.data.*;
import com.yanqu.road.entity.activity.langyabang.entity.LangYaTypePatrons;
import com.yanqu.road.entity.activity.langyabang.entity.LangYaUserBuilding;
import com.yanqu.road.entity.activity.langyabang.entity.LangYaUserEvent;
import com.yanqu.road.entity.activity.langyabang.entity.LangYaUserPatronsInfo;

import com.yanqu.road.entity.player.UserBaseInfo;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.pb.activity.LangYaBangProto;
import com.yanqu.road.pb.patrons.PatronsProto;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.utils.property.PropertyHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class LangYaBangPb {

    public static LangYaBangProto.LangYaBangDeclareWarSimpleInfo.Builder parseLangYaBangDeclareWarSimpleInfo(LangYaBangDeclareWarData declareWarData, UnionBaseInfo unionBaseInfo,UserBaseInfo userBaseInfo){
        LangYaBangProto.LangYaBangDeclareWarSimpleInfo.Builder builder =LangYaBangProto.LangYaBangDeclareWarSimpleInfo.newBuilder();
        builder.setDeclareWarUid(declareWarData.getDeclareWarUid());
        UnionProto.UnionBaseTempMsg.Builder unionMsg = UnionBasePb.parseUnionBaseTempMsg(unionBaseInfo);
        builder.setTargetUnionInfo(unionMsg);
        PlayerProto.PlayerBaseTempMsg.Builder userBaseInfoMsg= PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo);
        builder.setDeclareUser(userBaseInfoMsg);
        builder.setOpenUserRemainTimes(declareWarData.getOpenUserRemainTimes());
        builder.setOtherUserRemainTimes(declareWarData.getOtherUserRemainTimes());
        builder.setIsOver(declareWarData.getIsOver());
        builder.setUnionUid(declareWarData.getDefenseUnionUid());
        for (LangYaBangBeDeclareWarUserData data:declareWarData.getBeDeclareUserDataMap().values()){
            builder.addHasAttackUserId(data.getAttackUserId());
        }

        return builder;
    }

    public static List<LangYaBangProto.LangYaUserPatronsInfo> getUpdatePatronInfoMsgList(Map<Integer, UserPatrons>newPatronsInfoMap){
        List<LangYaBangProto.LangYaUserPatronsInfo> msgList = new ArrayList<>();
        for (Map.Entry<Integer, UserPatrons> entry : newPatronsInfoMap.entrySet()){
            LangYaBangProto.LangYaUserPatronsInfo.Builder builder = LangYaBangProto.LangYaUserPatronsInfo.newBuilder();
            builder.setPatrons(PatronsPb.parsePatronsSimpleTempMsg(entry.getValue()));
            builder.setPatronType(entry.getKey());
            builder.setDeclareWarAbility("0");
            msgList.add(builder.build());
        }
        return msgList;
    }




    public static Map<Integer, LangYaTypePatrons> parseMsgToPatronsInfoMap(List<LangYaBangProto.LangYaUserPatronsInfo> msgList){
        Map<Integer, LangYaTypePatrons> userPatronsInfoMap = new ConcurrentHashMap<>();
        for (LangYaBangProto.LangYaUserPatronsInfo patronsInfo:msgList){
            //类型
            LangYaTypePatrons langYaTypePatrons = new LangYaTypePatrons();
            langYaTypePatrons.setType(patronsInfo.getPatronType());
            langYaTypePatrons.setDeclareWarAttackAbility(BigInteger.ZERO);

            PatronsProto.PatronsSimpleTempMsg patrons = patronsInfo.getPatrons();
            LangYaUserPatronsInfo userPatronsInfo = parseMsgToLangYaUserPatronsInfo(patrons);

            langYaTypePatrons.setPatronsInfos(userPatronsInfo);

            userPatronsInfoMap.put(langYaTypePatrons.getType(),langYaTypePatrons);
        }
        return userPatronsInfoMap;
    }

    public static LangYaUserPatronsInfo parseMsgToLangYaUserPatronsInfo(PatronsProto.PatronsSimpleTempMsg patrons){
        LangYaUserPatronsInfo userPatronsInfo = new LangYaUserPatronsInfo();
        userPatronsInfo.setPatronsId(patrons.getPatronsId());
        userPatronsInfo.setLevel(patrons.getLevel());
        userPatronsInfo.setSkinId(patrons.getSkinId());
        userPatronsInfo.setStageLv(patrons.getStageLv());
        if (patrons.hasPromotionId()){
            userPatronsInfo.setPromotionId(patrons.getPromotionId());
        }else {
            userPatronsInfo.setPromotionId(0);
        }
        userPatronsInfo.setAbility(new BigInteger(patrons.getAbility()));
        return userPatronsInfo;

    }

    public static PatronsProto.PatronsSimpleTempMsg.Builder parseLangYaPatronToSimpleMsg(LangYaUserPatronsInfo userPatrons){
        PatronsProto.PatronsSimpleTempMsg.Builder msg = PatronsProto.PatronsSimpleTempMsg.newBuilder();
        msg.setPatronsId(userPatrons.getPatronsId());
        msg.setLevel(userPatrons.getLevel());
        msg.setStageLv(userPatrons.getStageLv());
        msg.setSkinId(userPatrons.getSkinId());
        msg.setPromotionId(userPatrons.getPromotionId());
        msg.setAbility(userPatrons.getAbility().toString());
        return msg;
    }

    public static List<LangYaBangProto.LangYaUserPatronsInfo> getPatronInfoMsgList(Map<Integer, LangYaTypePatrons> typePatronsMap){
        List<LangYaBangProto.LangYaUserPatronsInfo> langYaUserPatronsInfoList = new ArrayList<>();
        for (LangYaTypePatrons langYaTypePatrons:typePatronsMap.values()){
            LangYaBangProto.LangYaUserPatronsInfo.Builder builder = parseTypePatronMsg(langYaTypePatrons);
            langYaUserPatronsInfoList.add(builder.build());
        }
        return langYaUserPatronsInfoList;
    }

    public static LangYaBangProto.LangYaUserPatronsInfo.Builder parseTypePatronMsg(LangYaTypePatrons langYaTypePatrons){
        LangYaBangProto.LangYaUserPatronsInfo.Builder builder = LangYaBangProto.LangYaUserPatronsInfo.newBuilder();
        builder.setPatronType(langYaTypePatrons.getType());
        PatronsProto.PatronsSimpleTempMsg.Builder patronToSimpleMsg = parseLangYaPatronToSimpleMsg(langYaTypePatrons.getPatronsInfos());
        builder.setPatrons(patronToSimpleMsg);
        builder.setDeclareWarAbility(String.valueOf(langYaTypePatrons.getDeclareWarAttackAbility()));
        return builder;
    }


    public static LangYaBangProto.LangYaBangUserDataSyncMsg.Builder parseLangYaBangUserData(LangYaBangUserData langYaBangUserData, UserBaseInfo userBaseInfo,long unionScore){
        LangYaBangProto.LangYaBangUserDataSyncMsg.Builder builder = LangYaBangProto.LangYaBangUserDataSyncMsg.newBuilder();
        builder.setCoin(langYaBangUserData.getCoin());
        builder.setPower(langYaBangUserData.getPower());
        builder.setLastRecoveryPowerTime(langYaBangUserData.getLastRecoveryTime());
        if (langYaBangUserData.getUserPatronsInfoMap() != null){
            List<LangYaBangProto.LangYaUserPatronsInfo> patronInfoMsgList = getPatronInfoMsgList(langYaBangUserData.getUserPatronsInfoMap());
            builder.addAllPatronsInfo(patronInfoMsgList);
        }
        Map<Integer, Integer> buildingLevel = langYaBangUserData.getBuildingLevel();
        for (Map.Entry<Integer, Integer> entry: buildingLevel.entrySet()){
            LangYaBangProto.LangYaUserBuildingData.Builder buildingBuilder = LangYaBangProto.LangYaUserBuildingData.newBuilder();
            buildingBuilder.setBuildingId(entry.getKey());
            buildingBuilder.setLevelId(entry.getValue());
            builder.addBuildingData(buildingBuilder);
        }
        builder.setEventId(langYaBangUserData.getEventIdNow());
        LangYaBangProto.LangYaBangUserEventData.Builder langYaBangUserEventData = parseLangYaBangUserEventData(langYaBangUserData.getEvent(), userBaseInfo);
        builder.setEventDetail(langYaBangUserEventData);
        builder.setCoin(langYaBangUserData.getCoin());
        builder.setUnionScore(unionScore);
        builder.setOtherHelpReward(langYaBangUserData.getOtherGiveRewardNum());
        builder.setUserScore(langYaBangUserData.getScore());
        builder.setHistoryCoin(langYaBangUserData.getHistoryCoinNum());
        builder.setHistoryHighestPower(langYaBangUserData.getHistoryPower());
        builder.setTotalDrawTime(langYaBangUserData.getTotalDrawTimes());
        builder.addAllOpenSwitchType(langYaBangUserData.getOpenSwitchList());
        builder.setTotalUsedPower(langYaBangUserData.getHasAllUsedPower());
        return builder;
    }

    public static LangYaBangProto.LangYaBangUserEventData.Builder parseLangYaBangUserEventData(LangYaUserEvent event, UserBaseInfo userBaseInfo){
        LangYaBangProto.LangYaBangUserEventData.Builder eventBuilder = LangYaBangProto.LangYaBangUserEventData.newBuilder();
        eventBuilder.setTargetId(event.getTargetId());
        Map<Integer, LangYaUserBuilding> buildingMap = event.getTargetBuilding();
        if (buildingMap!=null){
            for (LangYaUserBuilding langYaUserBuilding: buildingMap.values()){
                LangYaBangProto.LangYaUserBuildingData.Builder buildingBuilder = LangYaBangProto.LangYaUserBuildingData.newBuilder();
                buildingBuilder.setBuildingId(langYaUserBuilding.getBuildingId());
                buildingBuilder.setLevelId(langYaUserBuilding.getLevelId());
                buildingBuilder.setOperateStatus(langYaUserBuilding.getOperateStatus());
                eventBuilder.addTargetBuilding(buildingBuilder);
            }
        }
        eventBuilder.setNpcAllBlood(event.getNpcAllBlood());
        eventBuilder.setNpcNowBlood(event.getNpcHasRobBlood());
        eventBuilder.setRemainOperateTime(event.getRemainOperateTime());
        if (userBaseInfo != null){
            eventBuilder.setUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(userBaseInfo));
        }
        eventBuilder.setPvpRandomType(event.getBattlePatronType());
        eventBuilder.setWinTime(event.getPvpWinTime());
        eventBuilder.setEventPower(event.getAddition());
        eventBuilder.setTemporaryReward(PropertyHelper.parsePropertyToString(event.getTemporaryReward()));
        eventBuilder.setMaxAttackTime(event.getMaxNpcAttackTime());
        return eventBuilder;
    }

    public static LangYaBangProto.LangYaBangCaseUserDataSyncMsg.Builder parseCaseGameData(LangYaUserCaseGameData userCaseGameData){
        LangYaBangProto.LangYaBangCaseUserDataSyncMsg.Builder builder = LangYaBangProto.LangYaBangCaseUserDataSyncMsg.newBuilder();
        builder.setCaseGameStatus(userCaseGameData.getCaseGameStatus());
        builder.setCaseGameNowCase(userCaseGameData.getCaseGameNowCase());
        builder.setCaseGameNowEvent(userCaseGameData.getCaseGameNowCard());
        for (int value:userCaseGameData.getCaseGameNowValue()){
            builder.addCaseGameNowValue(value);
        }
        for (Map.Entry<Integer,Integer> entry:userCaseGameData.getAllCaseData().entrySet()){
            LangYaBangProto.LangYaBangCaseGameCaseUserData.Builder caseBuilder = LangYaBangProto.LangYaBangCaseGameCaseUserData.newBuilder();
            caseBuilder.setCaseId(entry.getKey());
            caseBuilder.setCaseStatus(entry.getValue());
            builder.addCaseData(caseBuilder);
        }
        return builder;
    }

    public static LangYaUserBriefData parseUserBaseInfoToBriefData(UserBaseInfo baseInfo){
        LangYaUserBriefData briefData = new LangYaUserBriefData();
        if (baseInfo == null){
            return briefData;
        }
        briefData.setUserId(baseInfo.getUserAliasId());
        briefData.setSex(baseInfo.getSex());
        briefData.setNickName(baseInfo.getNickName());
        briefData.setDecoration(baseInfo.getDecoration());
        briefData.setHideVipAndAddress(baseInfo.getHideVipAndAddress());
        briefData.setVipExp(baseInfo.getVipExp());
        briefData.setServerId((int) baseInfo.getServerId());
        return briefData;
    }

    public static LangYaBangProto.LangYaBangBriefUserData.Builder parseBriefUserDataToMsg(LangYaUserBriefData briefData){
        LangYaBangProto.LangYaBangBriefUserData.Builder builder = LangYaBangProto.LangYaBangBriefUserData.newBuilder();
        builder.setUserId(briefData.getUserId());
        builder.setDecoration(briefData.getDecoration());
        builder.setSex(briefData.getSex());
        builder.setNickName(briefData.getNickName());
        builder.setServerId(briefData.getServerId());
        builder.setHideVipAndAddress(briefData.getHideVipAndAddress());
        builder.setVipExp(briefData.getVipExp());
        return builder;
    }

    public static LangYaUserBriefData parseMsgToBriefData(LangYaBangProto.LangYaBangBriefUserData msg){
        LangYaUserBriefData data = new LangYaUserBriefData();
        data.setUserId(msg.getUserId());
        data.setDecoration(msg.getDecoration());
        data.setSex(msg.getSex());
        data.setNickName(msg.getNickName());
        data.setServerId(msg.getServerId());
        data.setHideVipAndAddress(msg.getHideVipAndAddress());
        data.setVipExp(msg.getVipExp());
        return data;
    }



    public static LangYaBangProto.LangYaNoticeData.Builder parseNoticeMsg(LangYaBangWarReportData data){
        LangYaBangProto.LangYaNoticeData.Builder builder = LangYaBangProto.LangYaNoticeData.newBuilder();
        builder.setOtherId(data.getOtherUserId());
        builder.setOperateType(data.getOperateType());
        builder.setGoodReward(data.getGoodReward());
        builder.setCoinReward(data.getCoinReward());
        builder.setScoreChange(data.getScoreChange());
        builder.setCoinCost(data.getCostCoin());
        if (data.getOtherData() != null){
            builder.setUserData(parseBriefUserDataToMsg(data.getOtherData()));
        }
        builder.setOperateTime(data.getOperateTime());
        builder.setTime(data.getTime());
        builder.setSelfScoreChange(data.getSelfScoreChange());
        return builder;
    }

    public static LangYaBangWarReportData parseMsgToNoticeData(LangYaBangProto.LangYaNoticeData msg,int activityId,long userId){
        LangYaBangWarReportData data = new LangYaBangWarReportData();
        data.setActivityId(activityId);
        data.setUserId(userId);
        data.setOtherUserId(msg.getOtherId());
        data.setOperateType(msg.getOperateType());
        data.setOperateTime(msg.getOperateTime());
        if (msg.hasGoodReward()){
            data.setGoodReward(msg.getGoodReward());
        }
        data.setCoinReward(msg.getCoinReward());
        data.setScoreChange(msg.getScoreChange());
        data.setCostCoin(msg.getCoinCost());
        data.setOtherData(parseMsgToBriefData(msg.getUserData()));
        data.setTime(msg.getTime());
        data.setSelfScoreChange(msg.getSelfScoreChange());
        return data;
    }

    public static LangYaUnionBriefData parseUnionBriefData(String unionUid,UnionBaseInfo unionBaseInfo){
        LangYaUnionBriefData briefData = new LangYaUnionBriefData();
        briefData.setUnionUid(unionUid);
        briefData.setUnionName(unionBaseInfo.getUnionName());
        briefData.setUnionFlag(unionBaseInfo.getUnionFlag());
        briefData.setServerId(unionBaseInfo.getServerId());
        briefData.setUnionFlagStr(unionBaseInfo.getUnionFlagStr());
        briefData.setHonors(unionBaseInfo.getHonors());
        return briefData;
    }

    public static LangYaBangProto.LangYaBangUnionBriefData.Builder parseUnionBriefDataToMsg(LangYaUnionBriefData briefData){
        LangYaBangProto.LangYaBangUnionBriefData.Builder builder =LangYaBangProto.LangYaBangUnionBriefData.newBuilder();
        builder.setUnionUid(briefData.getUnionUid());
        builder.setUnionName(briefData.getUnionName());
        builder.setUnionFlag(briefData.getUnionFlag());
        builder.setServerId(briefData.getServerId());
        builder.setUnionFlagStr(briefData.getUnionFlagStr());
        builder.setHonors(briefData.getHonors());
        return builder;
    }




    public static LangYaBangProto.LangYaDeclareWarNoticeData.Builder parseNoticeMsg(LangYaDeclareWarNoticeData data){
        LangYaBangProto.LangYaDeclareWarNoticeData.Builder builder =LangYaBangProto.LangYaDeclareWarNoticeData.newBuilder();
        builder.setDeclareWarUid(data.getDeclareWarUid());
        builder.setAttackUser(parseBriefUserDataToMsg(data.getAttackUser()));
        builder.setAttackUnion(parseUnionBriefDataToMsg(data.getAttackUnion()));
        builder.setDefendUnion(parseUnionBriefDataToMsg(data.getDefendUnion()));
        builder.setTime(data.getTime());
        return builder;
    }

    public static String parseNoticeChatString(LangYaBangProto.LangYaDeclareWarNoticeData noticeData){
        String string = noticeData.getDeclareWarUid()+"|"+noticeData.getAttackUnion().getUnionUid()+";"+noticeData.getAttackUnion().getUnionName()+";"+noticeData.getAttackUnion().getUnionFlag()+";"+
                noticeData.getAttackUnion().getServerId()+";"+noticeData.getAttackUnion().getHonors()+";"+noticeData.getAttackUnion().getUnionFlagStr()+"|"+noticeData.getDefendUnion().getUnionUid()+";"+noticeData.getDefendUnion().getUnionName()+";"+noticeData.getDefendUnion().getUnionFlag()+";"+
                noticeData.getDefendUnion().getServerId()+";"+noticeData.getDefendUnion().getHonors()+";"+noticeData.getDefendUnion().getUnionFlagStr();
        return string;

    }

}
